package com.epfresh.supplier.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.daimajia.swipe.SwipeLayout;
import com.epfresh.api.helper.ApplicationHelper;
import com.epfresh.api.permission.HiPermission;
import com.epfresh.api.permission.PermissionCallback;
import com.epfresh.api.utils.T;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.CustomermGroupAdapter;
import com.epfresh.supplier.entity.CustomerGroupInfo;
import com.epfresh.supplier.entity.CustomerGroupItem;
import com.epfresh.supplier.widget.BaseItemDecoration;
import com.epfresh.supplier.widget.RecyclerViewAdapter;

import java.util.ArrayList;
import com.epfresh.supplier.activity.*;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.supplier.widget.*;
import com.epfresh.supplier.fragment.*;
import com.epfresh.supplier.entity.*;
import com.epfresh.api.global.*;

public class CustomerGroupContactActivity extends RequestCustomerListActivity implements ViewVariable {

    private TextView mTvTitle;
    private CustomerContactFragment mCustomerContactFragment;
    private CustomerGroupItem mGroupItem;
    private String name;
    private String mAction = "CustomerGroupContactActivity";
    //private ArrayList<SortModel> mContact;
    private String mEnterCustomerActivity;
    private LinearLayout mLlNoCustomers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_group_contact);
        initView();
        initFragment();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getCustomerList(/*ApplicationHelper.getInstance().getMerchantId() todo*/"", name, mGroupItem.getGroupId());
    }
    @Override
    protected View getToolBar() {
        return null;
    }

    private void initView() {
        mTvTitle = (TextView) findViewById(R.id.tv_group_contact_title);
        mLlNoCustomers = (LinearLayout) findViewById(R.id.ll_customer_contact_nobody);
        mGroupItem = getIntent().getParcelableExtra("GroupItem");
        mEnterCustomerActivity = getIntent().getStringExtra("enterCustomerActivity");
        mTvTitle.setText(mGroupItem.getGroupName());
        findViewById(R.id.back_ll).setOnClickListener(this);
        findViewById(R.id.iv_group_contact_add).setOnClickListener(this);

        findViewById(R.id.tv_add_new).setOnClickListener(this);
        findViewById(R.id.tv_add_book).setOnClickListener(this);
    }

    private void initFragment() {
        mCustomerContactFragment = CustomerContactFragment.newInstance();
        if (!mCustomerContactFragment.isAdded()) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_customer_contact, mCustomerContactFragment, "mCustomerContactFragment")
                    .commit();
        }
    }
    StringBuffer phoneList = new StringBuffer();

    @Override
    public void onClick(View v) {
        super.onClick(v);
        Intent main;
        switch(v.getId()){
/*            case R.id.iv_customer_info_add:
                if(findViewById(R.id.ll_add).getVisibility()==View.GONE){
                    findViewById(R.id.ll_add).setVisibility(View.VISIBLE);
                }else {
                    findViewById(R.id.ll_add).setVisibility(View.GONE);
                }
                break;*/
            case R.id.tv_add_book:
                HiPermission.create(this)
                        .checkSinglePermission(Manifest.permission.READ_CONTACTS, new PermissionCallback() {
                            @Override
                            public void onClose() {

                                T.toast("onClose");
                            }

                            @Override
                            public void onFinish() {

                                T.toast("onFinish");
                            }

                            @Override
                            public void onDeny(String permisson, int position) {
                                T.toast("onDeny");
                            }

                            @Override
                            public void onGuarantee(String permisson, int position) {
                                Intent main = new Intent(CustomerGroupContactActivity.this, ContactsActivity.class);
                                for(int i=0;i<mContact.size();i++){
                                    phoneList.append(mContact.get(i).getPhone()+",");
                                }
                                main.putExtra("phoneList",phoneList.toString());
                                main.putExtra("groupId",mGroupItem.getGroupId());
                                startActivityForResult(main,0x001);
                                findViewById(R.id.ll_add).setVisibility(View.GONE);
                            }
                        });
                break;
            case R.id.tv_add_new:
                findViewById(R.id.ll_add).setVisibility(View.GONE);
                main = new Intent(this, CustomerEditAddActivity.class);
                main.setAction(CUSTOMER_GROUP_CONTANCT_ACTIVITY);
                main.putExtra("GroupItem", mGroupItem);
                main.putExtra("enterCustomerActivity", mEnterCustomerActivity);//记录是从哪个界面进入客户列表
                main.putExtra("groupSize",groupSize);
                startActivityForResult(main,0x001);
                break;
            case R.id.back_ll:
                finish();
                break;
            case R.id.iv_group_contact_add:
                if(findViewById(R.id.ll_add).getVisibility()==View.GONE){
                    findViewById(R.id.ll_add).setVisibility(View.VISIBLE);
                }else {
                    findViewById(R.id.ll_add).setVisibility(View.GONE);
                }
/*                Intent group = new Intent(this, CustomerEditAddActivity.class);
                group.setAction(CUSTOMER_GROUP_CONTANCT_ACTIVITY);
                group.putExtra("GroupItem", mGroupItem);
                group.putExtra("enterCustomerActivity", mEnterCustomerActivity);//记录是从哪个界面进入客户列表
                group.putExtra("groupSize",groupSize);
                startActivityForResult(group,0x001);*/
                break;
        }
    }


    @Override
    protected void updateUI(ArrayList<SortModel> mContact) {
        //this.mContact = mContact;
        if (null == mContact || 0 == mContact.size()) {
            mLlNoCustomers.setVisibility(View.VISIBLE);
        } else {
            mLlNoCustomers.setVisibility(View.GONE);
            mCustomerContactFragment.transferData(mContact, mEnterCustomerActivity);
        }
    }
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        if(resultCode == 0x001)
            getCustomerList(null,"","");
    }
}
